<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
* Klasa Attributes controller obavlja validaciju podataka i komunicira sa modelom.
* 
* Vraca podatke korisnikckoj aplikaciji.
* @author NU SIT Bar
* @version 1.0
* @created 15-Aug-2012 1:41:14 PM
*/
class Attributes extends CI_Controller
{

    /**
    * Attribute type
    */
    var $type;
    /**
    * post_action, can be NEW || EDIT
    */
    var $action;

    function __construct(){
        parent::__construct();
        $this->load->model('codegen_model');
    }



    /**
    * Return view of all attributes for given type
    * 
    * @param post_type
    */
    function view_all($post_type)
    {
    }

    /**
    * 
    * @param ID
    */
    function view_edit($ID)
    {
    }

    /**
    * 
    * @param post_type
    */
    function view_new($post_type=NULL)
    {
        $this->data['page'] = 'attributes';
        $this->data['title'] = 'Novi atribut';
        $this->template->build('attributes/new_attribute',$this->data);
    }
    
    /**
    * 
    * @param post_type
    */
    function view_new_from_object($type)
    {
        $this->data['page'] = 'attributes';
        $this->data['title'] = 'Novi atribut';
        echo $this->load->view('attributes/new_attribute',$this->data,TRUE);
    }

    /**
    * 
    * @param post_action
    * @param post_ID
    */
    function submit_post()
    {

        if ($this->form_validation->run('attributes') == false)
        {
            echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
        } 
        else
        {                            

            unset($_POST['attr_value']);
            unset($_POST['post_action']);

            if ($this->codegen_model->add('attributes',$_POST) == TRUE)
            {
                echo json_encode(array('action'=>true,'msg'=>'Successful.','id'=>$this->db->insert_id()));
            }
            else
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>'<p>An Error Occured.</p>')); 

            }
        }

    }

    function delete($post_ID){

        /*if($this->owners_model->delete_owner($post_ID)){
        $this->view_all();
        }else{
        echo 'Error ocured';
        $this->view_all();
        }*/

    }
    
    function dropdown_check($str)
    {
        $CI =& get_instance();

        $CI->form_validation->set_message('dropdown_check', 'Please select %s.');
        
        return ($str=='0') ? FALSE : TRUE;
    }


}